<template>
  <div>
    <div style="padding: 5px 0">
      <el-input v-model="text" @keyup.enter.native="load" style="width: 200px"> <i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-button @click="add" type="primary" size="mini" style="margin: 10px">新增</el-button>
    </div>
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="100" sortable> </el-table-column>
      <el-table-column prop="bookId" label="ID "></el-table-column>
      <el-table-column prop="bookTitle" label="图书标题"></el-table-column>
      <el-table-column prop="bookIsbn" label="ISBN码"></el-table-column>
      <el-table-column prop="bookOldPrice" label="原价"></el-table-column>
      <el-table-column prop="bookNewPrice" label="现价"></el-table-column>
      <el-table-column prop="bookImages" label="图书图片"></el-table-column>
      <el-table-column prop="bookDescribe" label="描述"></el-table-column>
      <el-table-column prop="categoryId" label="分类ID"></el-table-column>
      <el-table-column prop="publisherId" label="出版社ID"></el-table-column>
      <el-table-column prop="bookState" label="上架状态"></el-table-column>
      <el-table-column prop="bookPubDate" label="出版日期"></el-table-column>
      <el-table-column prop="bookAuthors" label="作者"></el-table-column>
      <el-table-column prop="bookLanguage" label="版别"></el-table-column>
      <el-table-column prop="bookQuantityInput" label="入库数量"></el-table-column>
      <el-table-column prop="bookQuantityOutput" label="出库数量"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column prop="deleted" label="逻辑删除"></el-table-column>
      <el-table-column prop="version" label="乐观锁"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间"></el-table-column>

      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle  @click="edit(scope.row)"></el-button>
          <el-popconfirm
              @confirm="del(scope.row.id)"
              title="确定删除？"
          >
            <el-button type="danger" icon="el-icon-delete" circle slot="reference" style="margin-left: 10px"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[2, 5, 10]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 弹窗   -->
    <el-dialog title="信息" :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false">
      <el-form :model="entity">
        <el-form-item label="ID " label-width="150px">
          <el-input v-model="entity.bookId" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="图书标题" label-width="150px">
          <el-input v-model="entity.bookTitle" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="ISBN码" label-width="150px">
          <el-input v-model="entity.bookIsbn" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="原价" label-width="150px">
          <el-input v-model="entity.bookOldPrice" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="现价" label-width="150px">
          <el-input v-model="entity.bookNewPrice" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="图书图片" label-width="150px">
          <el-input v-model="entity.bookImages" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="150px">
          <el-input v-model="entity.bookDescribe" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="分类ID" label-width="150px">
          <el-input v-model="entity.categoryId" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="出版社ID" label-width="150px">
          <el-input v-model="entity.publisherId" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="上架状态" label-width="150px">
          <el-input v-model="entity.bookState" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="出版日期" label-width="150px">
          <el-date-picker style="width: 80%" v-model="entity.bookPubDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="作者" label-width="150px">
          <el-input v-model="entity.bookAuthors" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="版别" label-width="150px">
          <el-input v-model="entity.bookLanguage" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="入库数量" label-width="150px">
          <el-input v-model="entity.bookQuantityInput" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="出库数量" label-width="150px">
          <el-input v-model="entity.bookQuantityOutput" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="150px">
          <el-input v-model="entity.status" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="逻辑删除" label-width="150px">
          <el-input v-model="entity.deleted" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="乐观锁" label-width="150px">
          <el-input v-model="entity.version" autocomplete="off" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" label-width="150px">
          <el-date-picker style="width: 80%" v-model="entity.createTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" label-width="150px">
          <el-date-picker style="width: 80%" v-model="entity.updateTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import API from '../../utils/request'
const url = "/api/msslyyBook/"

export default {
  name: "MsslyyBook",
  data() {
    return {
      fileList: [],
      options: [],
      text: '',
      user: {},
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      entity: {},
      total: 0,
      dialogFormVisible: false
    };
  },
  created() {
    this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {}
    this.$emit('user', this.user);
    this.load()
  },
  methods: {
    fileSuccessUpload(res) {
      this.entity.file = "http://localhost:9999/files/" + res.data;
      this.fileList = [res.data]
      console.log(res)
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    load() {
       API.get(url + "/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.text
          }
       }).then(res => {
          this.tableData = res.data.records || []
          this.total = res.data.total
       })
    },
    add() {
      this.entity = {}
      this.fileList = []
      this.dialogFormVisible = true
    },
    edit(obj) {
      this.entity = JSON.parse(JSON.stringify(obj))
      this.fileList = []
      this.dialogFormVisible = true
    },
    save() {
      if (!this.entity.id) {
        API.post(url, this.entity).then(res => {
           if (res.code === '0') {
             this.$message({
               type: "success",
               message: "操作成功"
             })
           } else {
             this.$message({
               type: "error",
               message: res.msg
             })
           }
           this.load()
           this.dialogFormVisible = false
        })
      } else {
        API.put(url, this.entity).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: "操作成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()
          this.dialogFormVisible = false
        })
      }
    },
    del(id) {
      API.delete(url + id).then(res => {
        this.$message({
          type: "success",
          message: "操作成功"
        })
        this.load()
      })
    }
  },
};
</script>

<style scoped>
</style>
